﻿using System.Text.RegularExpressions;

namespace XExcel.Validations
{
    public class RegexValidate : Validate
    {
        public Regex Regexpression { get; }

        public RegexValidate(Regex regex, string message = null)
        {
            Regexpression = regex;
            Message = message ?? "格式不正确";
        }

        public RegexValidate(string pattern, string message = null)
        {
            Regexpression = new Regex(pattern);
            Message = message ?? "格式不正确";
        }

        public override bool Test(object val)
        {
            if(val == null || string.IsNullOrWhiteSpace(val.ToString()))
            {
                return true;
            }
            return Regexpression.IsMatch(val.ToString());
        }
    }
}
